-- 飞机类
Plane = {
    ImageNum = 0, -- 图形编号
    Image = nil, -- 图形
    origin_x = 0, -- 图形中心偏移
    origin_y = 0, -- 图形中心偏移
    x = 0, -- X坐标
    y = 0, -- Y坐标
    rotate = 0, -- 朝向角度
    speed = 0, -- 速度
    blinkLength = 0, -- 闪烁距离
    dashTime = 0, -- 冲刺时间
    dashSpeedRate = 0, -- 冲刺速度倍率
    dash_on = false, -- 冲刺状态
    dashCoolDown = 0, -- 冲刺冷却
    color = nil -- 飞机背景颜色RGB
}
-- 飞机图片
local Image = {"Plane_Black.png", "Plane_Blue.png", "Plane_Yellow.png"}
-- 创建飞机
function Plane:new(o)
    o = o or {}
    setmetatable(o, self)
    self.__index = self
    o.ImageNum = 1 -- 图形编号
    o.Image = love.graphics.newImage(Image[1]) -- 图形
    o.origin_x = o.Image:getWidth() / 2 -- 图形中心偏移
    o.origin_y = o.Image:getHeight() / 2 -- 图形中心偏移
    o.x = 500 -- X坐标
    o.y = 300 -- Y坐标
    o.rotate = 0 -- 朝向角度
    o.speed = 100 -- 速度
    o.blinkLength = 100 -- 闪烁距离
    o.dashTime = 10 -- 冲刺时间
    o.dashSpeedRate = 3 -- 冲刺速度倍率
    o.dash_on = false -- 冲刺状态
    o.dashCoolDown = 5 -- 冲刺冷却
    o.color = {255, 255, 255} -- 飞机背景颜色RGB
    return o
end
-- 飞机受地图边界限制
function Plane:mapLimit()
    if self.x > 700 then
        self.x = 700
    end
    if self.x < 0 then
        self.x = 0
    end
    if self.y > 500 then
        self.y = 500
    end
    if self.y < 0 then
        self.y = 0
    end
end
-- 飞机移动
function Plane:move(dt, mouse)
    if mouse.x and mouse.y then
        self.rotate = math.atan2((mouse.y - self.y), (mouse.x - self.x))
    end
    if love.keyboard.isDown("w") then
        self.y = self.y - dt * self.speed
    end
    if love.keyboard.isDown("s") then
        self.y = self.y + dt * self.speed
    end
    if love.keyboard.isDown("a") then
        self.x = self.x - dt * self.speed
    end
    if love.keyboard.isDown("d") then
        self.x = self.x + dt * self.speed
    end
end
-- 飞机绘制
function Plane:draw()
    love.graphics.setColor(love.math.colorFromBytes(self.color))
    love.graphics.draw(self.Image, self.x, self.y, self.rotate, 1, 1, self.origin_x, self.origin_y)
end
-- 飞机准星绘制
function Plane:sightDraw(mouse_x, mouse_y)
    love.graphics.setColor(love.math.colorFromBytes(0, 0, 0))
    love.graphics.setLineWidth(1)
    if (mouse_x >= self.x and (mouse_y - self.y) / (mouse_x - self.x) * (700 - self.x) + self.y <= 500) then
        love.graphics.line(self.x, self.y, 700, (mouse_y - self.y) / (mouse_x - self.x) * (700 - self.x) + self.y)
    elseif (mouse_y > self.y and (mouse_x - self.x) / (mouse_y - self.y) * (500 - self.y) + self.x < 700) then
        love.graphics.line(self.x, self.y, (mouse_x - self.x) / (mouse_y - self.y) * (500 - self.y) + self.x, 500)
    elseif (mouse_x < self.x) then
        love.graphics.line(self.x, self.y, (mouse_x - self.x) / (mouse_y - self.y) * (0 - self.y) + self.x, 0)
    else
        love.graphics.line(self.x, self.y, 0, (mouse_y - self.y) / (mouse_x - self.x) * (0 - self.x) + self.y)
    end
end
-- 改变飞机图片
function Plane:changeColor()
    self.ImageNum = self.ImageNum % #Image + 1
    self.Image = love.graphics.newImage(Image[self.ImageNum])
end
